package com.ljm.file.service.impl;

import com.ljm.file.properties.BucketType;
import com.ljm.file.service.FileService;
import com.ljm.file.util.AliOssUtil;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Author: ChenHuaMing
 * @Date: 2020/9/28 10:42
 * @Description: 阿里对象存储实现方式
 */
public class AliOssFileSerivceImpl implements FileService {
    private final String STORAGE_PATH_SPLIT="/";
    /**
     * 阿里对象存储工具
     */
    private AliOssUtil aliOssUtil;
    /**
     * 公开空间
     */
    private String publicBucketName;
    /**
     * 私密空间
     */
    private String secrecyBucketName;

    public AliOssFileSerivceImpl(String publicBucketName,String secrecyBucketName, AliOssUtil aliOssUtil) {
        this.aliOssUtil = aliOssUtil;
        this.publicBucketName=publicBucketName;
        this.secrecyBucketName=secrecyBucketName;
    }

    @Override
    public void fileUpload(BucketType bucketType, String ossFileName, InputStream inputStream) {
        fileUpload(getBucket(bucketType),null,ossFileName,inputStream);
    }

    @Override
    public void fileUpload(BucketType bucketType,String storagePath, String ossFileName, InputStream inputStream) {
        fileUpload(getBucket(bucketType),storagePath,ossFileName,inputStream);
    }

    @Override
    public void fileUpload(String bucketName, String storagePath, String ossFileName, InputStream inputStream) {
        ossFileName=handStoragePath(storagePath)+ossFileName;
        aliOssUtil.putObject(bucketName,ossFileName,inputStream);
    }

    @Override
    public void downLoadObjectToFile(BucketType bucketType,String objectName,String filePath) {
        downLoadObjectToFile(getBucket(bucketType),null,objectName,filePath);
    }

    @Override
    public void downLoadObjectToFile(BucketType bucketType,String storagePath, String objectName,String filePath) {
        downLoadObjectToFile(getBucket(bucketType),storagePath,objectName,filePath);
    }

    @Override
    public void downLoadObjectToFile(String bucketName, String storagePath, String ossFileName,String filePath) {
        ossFileName=handStoragePath(storagePath)+ossFileName;
        aliOssUtil.getObjectToFile(bucketName,ossFileName,filePath);
    }

    @Override
    public void downLoadObjectToOutputStream(BucketType bucketType, String ossFileName, OutputStream outputStream) throws IOException {
        downLoadObjectToOutputStream(getBucket(bucketType),null,ossFileName,outputStream);
    }

    @Override
    public void downLoadObjectToOutputStream(BucketType bucketType, String storagePath, String ossFileName, OutputStream outputStream) throws IOException {
        downLoadObjectToOutputStream(getBucket(bucketType),storagePath,ossFileName,outputStream);
    }

    @Override
    public void downLoadObjectToOutputStream(String bucketName, String storagePath, String ossFileName, OutputStream outputStream) throws IOException {
        ossFileName=handStoragePath(storagePath)+ossFileName;
        aliOssUtil.getObjectToOutputStream(bucketName,ossFileName,outputStream);
    }

    @Override
    public void deleteFile(BucketType bucketType,String objectName) {
        deleteFile(getBucket(bucketType),null,objectName);
    }

    @Override
    public void deleteFile(BucketType bucketType,String storagePath, String objectName) {
        deleteFile(getBucket(bucketType),storagePath,objectName);
    }

    @Override
    public void deleteFile(String bucketName, String storagePath, String ossFileName) {
        ossFileName=handStoragePath(storagePath)+ossFileName;
        aliOssUtil.deleteObject(bucketName,ossFileName);
    }

    @Override
    public String getDownLoadUrl(BucketType bucketType,String ossFileName,Long expirationTime) {
        return getDownLoadUrl(getBucket(bucketType),null,ossFileName,expirationTime);
    }

    @Override
    public String getDownLoadUrl(BucketType bucketType,String storagePath, String ossFileName,Long expirationTime) {
        return getDownLoadUrl(getBucket(bucketType),storagePath,ossFileName,expirationTime);
    }

    @Override
    public String getDownLoadUrl(String bucketName, String storagePath, String ossFileName,Long expirationTime) {
        ossFileName=handStoragePath(storagePath)+ossFileName;
        return aliOssUtil.getUrl(bucketName,ossFileName,expirationTime);
    }

    private String handStoragePath(String storagePath){
        if(StringUtils.isEmpty(storagePath)){
           return "";
        }
        if(storagePath.startsWith(STORAGE_PATH_SPLIT)){
            storagePath=storagePath.substring(1);
        }
        if(!storagePath.endsWith(STORAGE_PATH_SPLIT)){
            storagePath=storagePath+STORAGE_PATH_SPLIT;
        }
        return storagePath;
    }
    private String getBucket(BucketType bucketType){
        switch (bucketType){
            case publicBucket:
                if(StringUtils.isEmpty(publicBucketName)){
                    throw new RuntimeException("请配公开置储存空间");
                }
                return publicBucketName;
            case secrecyBucket:
                if(StringUtils.isEmpty(secrecyBucketName)){
                    throw new RuntimeException("请配私密置储存空间");
                }
                return secrecyBucketName;
                default:
                    throw new RuntimeException("请配置储存空间");
        }
    }
}
